#!/bin/bash

#将要用到的函数：
#
#get_return()
#get_confirm()
#set_menu_choice()
#insert_title()
#insert_track()
#add_record_tracks()
#add_records()
#find_cd()
#update_cd()
#count_cds()
#remove_records()
#list_tracks()

#全局变量
menu_choice=""
current_cd=""
title_file="title.cdb"
tracks_file="tracks.cdb"
temp_file=/tmp/cdb.$$
trap 'rm -f $temp_file' EXIT

#函数定义
get_return() {
    echo -e "Press return \c"
    read x
    return 0
}

get_confirm() {
    echo -e "Are you sure? \c"
    while true
    do
        read x
        case "$x" in
            y | yes | Y |Yes | YES )
                return 0;;
            n | no | N | No | No )
                echo
                echo "Cancelled"
                return 1;;
            *) echo "Please enter yes or no";;
        esac
    done
}

#主菜单函数
set_menu_choice() {
    clear
    echo "Options :-"
    echo
    echo "  a) Add new CD"
    echo "  f) Find CD"
    echo "  c) Count the CDs and tracks in the catalog"
    if [ "cdcatnum" != ""  ]; then
        echo "  l) List tracks on $cdtitle"
        echo "  r) Remove $cdtitle"
        echo "  u) Update track information for $cdtitle"
    fi
    echo "  q) Quit"
    echo
    echo -e "Please enter choice then press return \c"
    read menu_choice
    return 
}

insert_title() {
    echo $* >> $title_file
    return
}

insert_track() {
    echo $* >> $tracks_file
    return
}

add_record_tracks() {
    echo "Enter track information for this CD"
    echo "When no more tracks enter q"
    cdtrack=1
    cdttitle=""
    while [ "$cdttitle" != "q"]
    do
        echo -e "Track $cdtrack. track title? \c"
        read tmp
        cdttitle=${tmp%%,*}
        if [ "$tmp" =!  "$cdttitle" ]; then
            echo "Sorry, no commas allowed"
            continue
        fi
        if [ -n "$cdttitle" ]; then
            if [ "$cdttitle" != "q" ]; then
                insert_track $cdcatnum,$cdtrack,$cdttitle
            fi
        else
            cdtrack=$((cdtrack-1))
        fi
        cdtrack=$((cdtrack+1))
    done
}
    
#输入新CD唱片的标题信息

